package com.jingdianjichi.auth.common.entity;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * 分页返回实体
 */
public class PageResult<T> implements Serializable {
    private Integer pageNo = 1;
    private Integer pageSize = 20;
    private Integer total = 0;
    private Integer totalPage = 0;
    private List<T> result = Collections.emptyList();
    /**
     * 所在页的起始索引
     */
    private Integer start = 1;
    /**
     * 所在页的结束index
     */
    private Integer end = 0;

    public void setRecords(List<T> result) {
        this.result = result;
//        if (result != null && result.size() > 0) {
//            setTotal(result.size());
//        }
    }

    public List<T> getResult() {
        return this.result;
    }

    public Integer getPageSize() {
        return this.pageSize;
    }

    public Integer getPageNo() {
        return this.pageNo;
    }

    public Integer getTotal(){
        return this.total;
    }

    public Integer getTotalPage(){
        return this.totalPage;
    }

    public void setTotal(Integer total) {
        this.total = total;
        if (this.pageSize > 0) {
            this.totalPage = (total / this.pageSize) + (total % this.pageSize == 0 ? 0 : 1);
        } else {
            this.totalPage = 0;
        }
        this.start = (this.pageSize > 0 ? (this.pageNo - 1) * pageSize : 0) + 1;
//        this.end = this.start - 1 + this.pageSize * (this.pageNo > 0 ? 1 : 0);
        this.end = pageNo.equals(totalPage) ? this.start + total % this.pageSize - 1 : this.start + this.pageSize - 1;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

}
